<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>

<%@include file="/common/head.jsp" %>

<c:set var="module" value="user"/>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                    	<form role="form" class="form-inline" id="query-form" method="post" action="${ctx }/admin/user/list" >
                    		<!-- 查询开始 -->
                              <input type="hidden" name="onlyQuery" value="${onlyQuery }"/>
                              <input type="hidden" name="type" value="${type }" />
                              <input type="hidden" name="fromType" value="${fromType }" />
                              <div class="form-group">
                                <label for="query-user" class="sr-only">用户</label>
				                 	<input type="text" placeholder="关键词" name="searchTerm" value="${query.searchTerm }" class="form-control"/> 
                            </div>
                            
                            <c:if test="${empty type }">
                            <div class="form-group">
                                <label for="query-user" class="sr-only">部门</label>
                            		<select name="department.id" id="" class="form-control">
                            			<option value="">请选择部门</option>
                            			<c:forEach items="${departments }" var="d">
                            			<option value="${d.id }" ${query.department.id eq d.id ? 'selected' : '' }>${d.name }</option>
                            			</c:forEach>
                            		</select> 
                            </div>
                            </c:if>
                            
                            <div class="form-group">
                 <div class="col-sm-10">
					<select name="province.id" class="form-control" id="provinceId">
						<option value="-1">请选择省</option>
						<c:forEach items="${provinces }" var="p">
						<option value="${p.id }" ${query.province.id eq p.id ? 'selected' : '' }>${p.name }</option>
						</c:forEach>
					</select>						                 	
                 </div>
                 
             </div>	
			<div class="form-group">
                 <div class="col-sm-10">
                 	<select name="city.id" class="form-control" id="cityId">
                 	<option value="-1" >请选择城市</option>
                 		<c:if test="${not empty(query.city.id) }">
                 		<option value="${query.city.id }" >${query.city.name }</option>
                 		</c:if>
					</select>
					<input type="hidden" id="selectCity" value="${query.city.id }"/>	
                 </div>
             </div>	
                            
                            <div class="form-group">
                            	<button class="btn btn-primary form-control" type="submit"><i class="fa fa-check"></i>&nbsp;查询</button>
                            </div>
                            <!-- 查询结束 -->
                        </form>
                    
						<div>
							<p>
		                        <c:if test="${not onlyQuery }">
		                        <button type="button" class="btn btn-outline btn-primary" onclick="add()">新增</button>
		                        
		                        
		                        
		                        <button type="button" class="btn btn-outline btn-info" onclick="edit()">详情</button>
		                        
		                        
		                        
		                        <button type="button" class="btn btn-outline btn-danger" onclick="del();">删除</button>
		                        
		                        
		                        
		                        <button type="button" class="btn btn-outline btn-primary" onclick="auth()">角色</button>
		                        
		                        
		                        
		                        <button type="button" class="btn btn-outline btn-info" onclick="resetPwd()">重置密码</button>
		                        
		                        </c:if>
		                        <!-- 工厂 -->
		                        <c:if test="${type eq 6 }">
		                        <button type="button" class="btn btn-outline btn-info" onclick="setPrice()">价格设置</button>
		                        </c:if>
		                        <c:if test="${type eq 10}">
		                        <button type="button" class="btn btn-outline btn-info" onclick="setDepartment()">部门设置</button>
		                        </c:if>
		                    </p>
						</div>
						<table id="table" data-toggle="table" data-height="400" data-click-to-select="true" data-mobile-responsive="true">
                                    <thead>
                                        <tr>
                                            <th data-field="state" data-checkbox="true"></th>
                                            <th data-field="id">id</th>
                                            <th data-field="type">类型</th>
                                            <th data-field="username">用户名</th>
                                            <th data-field="realName">真实姓名</th>
                                            <th data-field="mobile">电话</th>
                                            <th data-field="displayName" hidden="true">显示名称</th>
                                            <!-- <th data-field="password">密码</th> -->
                                            <c:if test="${type eq 10}">
                                            <th data-field="department">部门</th>
                                            </c:if>
                                            <th data-field="headImage">头像</th>
                                            <th data-field="sex">性别</th>
                                            <th data-field="province">省</th>
                                            <th data-field="city">城市</th>
                                            <c:if test="${type eq 5 or not empty query.parent.id}">
                                            <th data-field="operation">操作</th>
                                            </c:if>
                                        </tr>
                                    </thead>
							<tbody>
								<c:forEach items="${list }" var="n" varStatus="idx">
								<tr data-index="${idx.index }">
									<td class="bs-checkbox">
										<input data-index="0" name="btSelectItem" type="checkbox">
									</td>
									<td style="">${n.id }</td>
									<td style="">${enum:list('UserType', n.type)}</td>
									<td style="">${n.username }</td>
									<td style="">${n.realName }</td>
									<td style="">${n.mobile }</td>
									<td style="">${n.displayName }</td>
									<c:if test="${type eq 10}">
									<td>${n.department.name }</td>
									</c:if>
									<td style=""><img src="${n.headImage}" width="100px"/></td>
									<td style="">${n.sex==1?'男':'女' }</td>
									<td style="">${n.province.name}</td>
									<td style="">${n.city.name}</td>
									<c:if test="${type eq 5 or not empty n.parent.id}">
									<td style="">
										<c:if test="${type eq 5}">
										<a href="javascript:void(0);" onclick="subCount('${n.id}', '${n.city.id }');" class="btn">子帐号</a>
										</c:if>						
										<c:if test="${not empty query.parent.id}">
										<a href="javascript:void(0);" onclick="salesCount('${n.id}');" class="btn">销量统计</a>
										</c:if>				
									</td>
									</c:if>
								</tr>
								</c:forEach>
							</tbody>
						</table>
                   
						<div class="row">
							<div class="col-sm-6">
								<div class="dataTables_info" id="DataTables_Table_0_info"
									role="alert" aria-live="polite" aria-relevant="all">显示第 ${(page.page - 1) * page.pageSize  + 1}
									到第 ${(page.page) * page.pageSize} 项，共 ${page.totalRecords } 项</div>
							</div>
							<div class="col-sm-6">
								<div class="dataTables_paginate paging_simple_numbers"
									id="DataTables_Table_0_paginate">
									<ul class="pagination" id="pagination">
									</ul>
								</div>
							</div>
						</div>

					</div>
                </div>
            </div>
        </div>
    </div>

<%@include file="/common/foot.jsp" %>
<script>
function salesCount(userId) {
	layer.open({
	    type: 2,
	    skin: 'layui-layer-rim', //加上边框
	    area: [layer_default_width, layer_default_height], //宽高
	    content: "${ctx}/admin/index/index_v3",
	    btn: ['确定', '取消'],
	    yes: function(index, layero){ //或者使用btn1
	    	layer.close(index);
	    }, cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    	layer.close(index);	
	    }
	});
}
function subCount(parentId, cityId) {
	layer.open({
	    type: 2,
	    skin: 'layui-layer-rim', //加上边框
	    area: [layer_big_width, layer_big_height], //宽高
	    content: "${ctx}/admin/user/list?city.id=" + cityId + "&excludeSelf=true&excludeUserId=" + parentId + "&querySubacount=true",
	    btn: ['确定', '取消'],
	    yes: function(index, layero){ //或者使用btn1
	    	layer.close(index);
	    }, cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	});
}
function add() {
		layer.open({
		    type: 2,
		    skin: 'layui-layer-rim', //加上边框
		    area: [layer_default_width, layer_default_height], //宽高
		    content: "${ctx}/admin/user/detail?type=${type}&parentId=${parentId}",
		    btn: ['确定', '取消'],
		    yes: function(index, layero){ //或者使用btn1
		    	var detailForm = layer.getChildFrame('form', index);
		    	console.log(detailForm);
		    	detailForm.ajaxSubmit({
		    		success: function(data) {
		    			if (isSuccess(data)) {
		    				layer.close(index);
		    				reload();
		    			} else {
		    			}
		    		}
		    	});
		    }, cancel: function(index){ //或者使用btn2
		        //按钮【按钮二】的回调
		    }
		});
}

function setDepartment() {
	var selections = $('#table').bootstrapTable('getAllSelections');
	if (selections.length == 0) {
		error('请选择一条记录');
		return;
	} else if (selections.length > 1) {
		error('只能选择一条记录');
		return;
	}
	var id = selections[0].id;
	
	layer.open({
	    type: 2,
	    skin: 'layui-layer-rim', //加上边框
	    area: [layer_default_width, layer_default_height], //宽高
	    content: "${ctx}/admin/user/setDepartment?id=" + id,
	    btn: ['确定', '取消'],
	    yes: function(index, layero){ //或者使用btn1
	    	var detailForm = layer.getChildFrame('form', index);
	    	console.log(detailForm);
	    	detailForm.ajaxSubmit({
	    		success: function(data) {
	    			if (isSuccess(data)) {
	    				layer.close(index);
	    				reload();
	    			} else {
	    			}
	    		}
	    	});
	    }, cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	});
}

function edit() {
	var selections = $('#table').bootstrapTable('getAllSelections');
	if (selections.length == 0) {
		error('请选择一条记录');
		return;
	} else if (selections.length > 1) {
		error('只能选择一条记录');
		return;
	}
	var id = selections[0].id;
	
	layer.open({
	    type: 2,
	    skin: 'layui-layer-rim', //加上边框
	    area: [layer_default_width, layer_default_height], //宽高
	    content: "${ctx}/admin/user/detail?id=" + id,
	    btn: ['确定', '取消'],
	    yes: function(index, layero){ //或者使用btn1
	    	var detailForm = layer.getChildFrame('form', index);
	    	console.log(detailForm);
	    	detailForm.ajaxSubmit({
	    		success: function(data) {
	    			if (isSuccess(data)) {
	    				layer.close(index);
	    				reload();
	    			} else {
	    			}
	    		}
	    	});
	    }, cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	});
}
var page = "${page.page}";
function reload() {
	page_submit(page);
}
function del() {
	var selections = $('#table').bootstrapTable('getAllSelections');
	if (selections.length < 1) {
		error('请选择要删除的记录');
		return;
	}
	layer.confirm('确认要删除吗?', {icon: 3, title:'提示'}, function(index){
		var ids = new Array();
		for (var i = 0; i < selections.length; i++) {
			ids.push(selections[i].id);
		}
		$.ajax({
			url: "${ctx}/admin/user/delete",
			data: {
				ids: ids.join(',')
			},
			success: function(data) {
				if (isSuccess(data)) {
					layer.close(index);
					reload();
				}
			}
		});
	});
	
}
function auth(){
	var selections = $('#table').bootstrapTable('getAllSelections');
	if (selections.length == 0) {
		error('请选择一条记录');
		return;
	} else if (selections.length > 1) {
		error('只能选择一条记录');
		return;
	}
	var id = selections[0].id;
	layer.open({
	    type: 2,
	    skin: 'layui-layer-rim', //加上边框
	    area: [layer_default_width, layer_default_height], //宽高
	    content: "${ctx}/admin/user/auth?userId=" + id,
	    success: function(layero, index){
	    	var roleTable = layer.getChildFrame("#roleTable", index);
	    	var els = roleTable.find('input[name="btSelectItem"]:checked');
	    	var selectedIndex = [];
	    	els.each(function(){
	    		var index = $(this).parent().parent().index();
	    		selectedIndex.push(index);
	    	});
	    	roleTable.bootstrapTable();
	    	for(var i=0; i<selectedIndex.length; i++) {
	    		roleTable.bootstrapTable('check', selectedIndex[i]);
	    	}
	    },
	    btn: ['确定', '取消'],
	    yes: function(index, layero){ //或者使用btn1
	    	var table = layer.getChildFrame("#roleTable", index);
	    	var sels = table.bootstrapTable('getSelections');
	        if (sels.length < 1) {
	    		error('请选择要赋权限的记录');
	    		return;
	    	}
	    	var ids = new Array();
			for (var i = 0; i < sels.length; i++) {
				ids.push(sels[i].id);
			}
			$.ajax({
				url: "${ctx}/admin/user/authSave",
				data: {
					ids: ids.join(','),
					userId : id
				},
				success: function(data) {
					if (isSuccess(data)) {
						layer.close(index);
						reload();
					}
				}
			});
	    }, cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    } 
	});
}
paging('pagination', ${page.totalPages}, ${page.page});

function resetPwd() {
	var selections = $('#table').bootstrapTable('getAllSelections');
	if (selections.length == 0) {
		error('请选择一条记录');
		return;
	} else if (selections.length > 1) {
		error('只能选择一条记录');
		return;
	}
	var id = selections[0].id;
	
	layer.open({
	    type: 2,
	    skin: 'layui-layer-rim', //加上边框
	    area: [layer_default_width, layer_default_height], //宽高
	    content: "${ctx}/admin/user/reset?userId=" + id,
	    btn: ['确定', '取消'],
	    yes: function(index, layero){ //或者使用btn1
	    	var resetForm = layer.getChildFrame('#reset-form', index);
	    	console.log(resetForm);
	    	var newPassword = $('#newPassword', resetForm).val();
	    	var confirmPassword = $('#confirmPassword', resetForm).val();
	    	if(!!!newPassword){alert('请输入新密码.');return false;}
	    	if(!!!confirmPassword){alert('请输入确认新密码.');return false;}
	    	if(newPassword != confirmPassword){alert('两次输入密码不匹配.');return false;}
	    	resetForm.ajaxSubmit({
	    		success: function(data) {
	    			if (isSuccess(data)) {
	    				layer.close(index);
	    				reload();
	    			} else {
	    				
	    			}
	    		}
	    	});
	    }, cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	});
}

function setPrice() {
	var selections = $('#table').bootstrapTable('getAllSelections');
	if (selections.length == 0) {
		error('请选择一条记录');
		return;
	} else if (selections.length > 1) {
		error('只能选择一条记录');
		return;
	}
	var id = selections[0].id;
	
	layer.open({
	    type: 2,
	    skin: 'layui-layer-rim', //加上边框
	    area: [layer_default_width, layer_default_height], //宽高
	    content: "${ctx}/admin/user/setPrice?userId=" + id,
	    btn: ['确定', '取消'],
	    yes: function(index, layero){ //或者使用btn1
	    	var f = layer.getChildFrame('form', index);
	    	f.ajaxSubmit({
	    		success: function(data) {
	    			if (isSuccess(data)) {
	    				layer.close(index);
	    				reload();
	    			} else {
	    			}
	    		}
	    	});
	    }, cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	});
}

$('#provinceId').change(function() {
	var provinceId = $(this).val();
	$('#cityId').html('');
	$('#districtId').html('');
	if (provinceId != '') {
		$.ajax({
			url: "${ctx}/admin/province/findArea?provinceId=" + provinceId,
			dataType: "json",
			success: function(array) {
				$('#cityId').append('<option value="-1">请选择城市</option>');
				for (var i = 0; i < array.length; i++) {
					var obj = array[i];
					var option = '<option value="' + obj.id + '">' + obj.name + '</option>';
					var cityId=$('#selectCity').val();
					if(null!==cityId&&""!=cityId&&cityId==obj.id)
					  option = '<option value="' + obj.id + '" selected="selected">' + obj.name + '</option>';
					$('#cityId').append(option);	
				}
			}
		});
	}
	
});

$('#provinceId').trigger("change"); 
</script>
</body>
</html>